import { getOrderDetail, editOrder, uploadFile } from "../../api/index";
import { store } from "../../../store/index";

Page({
  data: {
    description: '',
    fileList: [],
    id: '',
    canSubmit: false,
    formData: {
      description: '',
      fileList: []
    }
  },
  onLoad(options) {
    this.setData({
      id: options.id
    });
  },

  // 监听输入变化
  onDescriptionChange(event) {
    console.log(event, 'event.value');
    this.setData({
      formData: {
        ...this.data.formData,
        description: event.detail.value
      },
      canSubmit: event.detail.value || this.data.fileList.length > 0
    });
  },

  // 上传图片后回调
  afterRead(event) {

    const { file } = event.detail;
    uploadFile({...file, file: file.url}).then(res => {
      console.log(res, 'resresresres');
      this.setData({ 
        formData: {
          ...this.data.formData,
          fileList: [...this.data.formData.fileList, file]
        },
        fileList: [...this.data.fileList, res],
        canSubmit: true 
      });

    });


  },

  // 删除图片
  deleteImage(event) {

    this.setData({ 
      formData: {
        ...this.data.formData,
        fileList: []
      },
      fileList: [],
      canSubmit: false
    });
  },

  // 提交任务
  onSubmit() {
    console.log(this.data.formData, 'this.data.formData');
    
    if (!this.data.canSubmit) return;
    
    // TODO: 这里处理提交逻辑
    wx.showLoading({
      title: '提交中...',
    });

    editOrder({
      taskFinishImg: this.data.fileList[0],
      taskFinish: this.data.formData.description,
      id: this.data.id,
      status: '接单员确认完成'
    }).then(res => {
      wx.showToast({
        title: '提交成功',
        icon: 'success'
      });
      wx.navigateBack();
      wx.hideLoading();
    });
  }
});